import sys
import os
sys.path.insert(0, os.path.abspath('../'))
sys.path.insert(0, os.path.abspath('../../'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/landscape/'))
sys.path.insert(0, os.path.abspath('/home/hm-tlacherm/qlm_notebooks/notebooks_1.2.1/notebooks/master_thesis_qaoa/ibm/landscape/simulator/'))
import numpy as np
import qiskit
provider = qiskit.IBMQ.load_account()
from qiskit import Aer
from qiskit.utils import QuantumInstance
from qiskit_optimization.algorithms import MinimumEigenOptimizer
from qiskit.algorithms import QAOA
from shared.QiskitMaxcut import *
from ibm.ibm_parameters import *
from matplotlib import pyplot as plt
%matplotlib inline
from ibm_landscape_processes import *
%load_ext autoreload
%autoreload 2
# ---- Define graph and MaxCut ----
graph = generate_butterfly_graph(with_weights=True)
opt_max_cut = -35
max_cut = Maxcut(graph, opt_max_cut)
max_cut_qubo = max_cut.to_qubo()
max_cut.draw()
from qiskit_optimization.algorithms import GoemansWilliamsonOptimizer
optimizer = GoemansWilliamsonOptimizer(5)
result = optimizer.solve(max_cut_qubo)
mean, distribution = max_cut.analyse(result, print_output=True)
max_cut.plot_histogram(distribution, mean)
optimal function value: 35.0 optimal value: [0 1 0 1 0] status: SUCCESS 00011: value: -32.000, probability: 25.0% 00101: value: -32.000, probability: 25.0% 01110: value: -33.000, probability: 25.0% 01010: value: -35.000, probability: 25.0% Expectation Value: -33.0 Highest Exp.Value: -32 with 50.0 % Lowest Exp.Value: -35 with 25.0 % Highest Probability: -32 with 50.0 % Ratio r: 0.3333333333333333 Approiximation ratio: 0.9428571428571428 MSE: 1.6666666666666667 RMSE: 1.2909944487358056
from landscape_helper import *
landscape_results_filename = f"landscape_simulator_{graph.name}_results.npy"
landscape = load_landscape_data(landscape_results_filename, max_cut)
Landscape does not exists, start calculating.. Row 0 Row 1 Row 2 Row 3 Row 4 Row 5 Row 6 Row 7 Row 8 Row 9 Row 10 Row 11 Row 12 Row 13 Row 14 Row 15 Row 16 Row 17 Row 18 Row 19 Row 20 Row 21 Row 22 Row 23 Row 24 Row 25 Row 26 Row 27 Row 28 Row 29 Row 30 Row 31 Row 32 Row 33 Row 34 Row 35 Row 36 Row 37 Row 38 Row 39 Row 40 Row 41 Row 42 Row 43 Row 44 Row 45 Row 46 Row 47 Row 48 Row 49 Row 50 Row 51 Row 52 Row 53 Row 54 Row 55 Row 56 Row 57 Row 58 Row 59 Row 60 Row 61 Row 62
min_beta, min_gamma, min_exp = describe_landscape(landscape)
Landscape mean: -23.57 Min Exp.Value: -32.37 Min. Beta: -2.34, Min. Gamma: -2.94
from plot_helper import *
# Plot landscape in 3D
plot_landscape_3d(landscape)
# Plot Heatmap
heatmap = plot_heatmap(landscape)
heatmap = display_minimum(heatmap, min_gamma, min_beta, min_exp)
from tqa import calculate_tqa
p = 1
tqa_initial_points = calculate_tqa(graph, p)
TQA: Beta: [0.45175879], Gamma: [0.45175879] (p=1)
from qaoa_helper import *
qnSpsa = QNSPSA(calculate_fidelity(create_qaoa(), max_cut_qubo), maxiter=50)
optimizers = [
('COBYLA',COBYLA(maxiter=50)),
('SPSA', SPSA(maxiter=50)),
('QNSPSA', qnSpsa),
]
for optimizer_tuple in optimizers:
optimizer_name, optimizer = optimizer_tuple
# create qaoa
qaoa = create_qaoa(optimizer = optimizer,
reps=p,
initial_point=tqa_initial_points,
with_callback=True
)
# run qaoa
result, optimal_parameters, optimizer_history = run_qaoa_with_callback(qaoa, max_cut_qubo)
# analyse results
print(f"{optimizer_name} Optimizer")
mean, distribution = max_cut.analyse(result, print_output=True)
max_cut.plot_histogram(distribution, mean)
optimizer_history[2][-1] = mean
print()
# Display Optimizer Results
counts, energy_values, maxcut_values, optimizer_gammas, optimizer_betas = optimizer_history
display_optimizer_path(heatmap, optimizer_gammas, optimizer_betas, maxcut_values, optimizer_name)
# Plot Optimizer History MaxCut Evaluation # Values from landscape -> real values may deviate
plot_optimizer_maxcut_history(counts, maxcut_values, optimizer_name)
# Plot Optimizer History Energy Evaluation -> not MaxCutMean!
plot_optimizer_energy_history(counts, energy_values, optimizer_name)
COBYLA Optimizer optimal function value: -35.0 optimal value: [0. 1. 0. 1. 0.] status: SUCCESS Number of samples (32) is too large to display. Skip. Expectation Value: -28.922250000000005 Highest Exp.Value: 0 with 2.325 % Lowest Exp.Value: -35 with 1.4625000000000001 % Highest Probability: -32 with 30.725 % Ratio r: 0.8263500000000001 Approiximation ratio: 0.8263500000000001 MSE: 137.36362198557697 RMSE: 11.72022277883731
SPSA Optimizer optimal function value: -35.0 optimal value: [0. 1. 0. 1. 0.] status: SUCCESS Number of samples (32) is too large to display. Skip. Expectation Value: -23.44375 Highest Exp.Value: 0 with 6.987500000000001 % Lowest Exp.Value: -35 with 5.675 % Highest Probability: -32 with 18.125 % Ratio r: 0.6698214285714286 Approiximation ratio: 0.6698214285714286 MSE: 106.70172175480768 RMSE: 10.329652547632358
QNSPSA Optimizer optimal function value: -35.0 optimal value: [0. 1. 0. 1. 0.] status: SUCCESS Number of samples (32) is too large to display. Skip. Expectation Value: -29.594875000000005 Highest Exp.Value: 0 with 1.8124999999999998 % Lowest Exp.Value: -35 with 12.337 % Highest Probability: -32 with 29.2 % Ratio r: 0.8455678571428573 Approiximation ratio: 0.8455678571428573 MSE: 145.26554934254816 RMSE: 12.052615871359551
eval_num = 50
max_p = 10
all_results = {}
from qaoa_helper import *
qaoa_p_means = []
qaoa_p_ratios = []
qaoa_p_approx_ratios = []
for p in range(1,max_p+1):
m, r, ar = start_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p)
qaoa_p_means.append(m)
qaoa_p_ratios.append(r)
qaoa_p_approx_ratios.append(ar)
all_results["QAOA"] = [qaoa_p_means, qaoa_p_ratios, qaoa_p_approx_ratios]
p=1: .................................................. p=2: .................................................. p=3: .................................................. p=4: .................................................. p=5: .................................................. p=6: .................................................. p=7: .................................................. p=8: .................................................. p=9: .................................................. p=10: ..................................................
display_boxplots_results(qaoa_p_means, qaoa_p_ratios, qaoa_p_approx_ratios)
from qaoa_helper import *
tqa_p_means = []
tqa_p_ratios = []
tqa_p_approx_ratios = []
tqa_init_points = []
for p in range(1,max_p+1):
tqa_initial_points = calculate_tqa(graph, p)
m, r, ar = start_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p, init_points=tqa_initial_points)
tqa_p_means.append(m)
tqa_p_ratios.append(r)
tqa_p_approx_ratios.append(ar)
tqa_init_points.append(tqa_initial_points)
all_results["TQA QAOA"] = [tqa_p_means, tqa_p_ratios, tqa_p_approx_ratios, tqa_init_points]
TQA: Beta: [0.45175879], Gamma: [0.45175879] (p=1) p=1: .................................................. TQA: Beta: [0.78712312 0.26237437], Gamma: [0.26237437 0.78712312] (p=2) p=2: .................................................. TQA: Beta: [0.9586823 0.57520938 0.19173646], Gamma: [0.19173646 0.57520938 0.9586823 ] (p=3) p=3: .................................................. TQA: Beta: [0.875 0.625 0.375 0.125], Gamma: [0.125 0.375 0.625 0.875] (p=4) p=4: .................................................. TQA: Beta: [0.72 0.56 0.4 0.24 0.08], Gamma: [0.08 0.24 0.4 0.56 0.72] (p=5) p=5: .................................................. TQA: Beta: [0.61111111 0.5 0.38888889 0.27777778 0.16666667 0.05555556], Gamma: [0.05555556 0.16666667 0.27777778 0.38888889 0.5 0.61111111] (p=6) p=6: .................................................. TQA: Beta: [0.53061224 0.44897959 0.36734694 0.28571429 0.20408163 0.12244898 0.04081633], Gamma: [0.04081633 0.12244898 0.20408163 0.28571429 0.36734694 0.44897959 0.53061224] (p=7) p=7: .................................................. TQA: Beta: [0.46875 0.40625 0.34375 0.28125 0.21875 0.15625 0.09375 0.03125], Gamma: [0.03125 0.09375 0.15625 0.21875 0.28125 0.34375 0.40625 0.46875] (p=8) p=8: .................................................. TQA: Beta: [0.41975309 0.37037037 0.32098765 0.27160494 0.22222222 0.17283951 0.12345679 0.07407407 0.02469136], Gamma: [0.02469136 0.07407407 0.12345679 0.17283951 0.22222222 0.27160494 0.32098765 0.37037037 0.41975309] (p=9) p=9: .................................................. TQA: Beta: [0.38 0.34 0.3 0.26 0.22 0.18 0.14 0.1 0.06 0.02], Gamma: [0.02 0.06 0.1 0.14 0.18 0.22 0.26 0.3 0.34 0.38] (p=10) p=10: ..................................................
display_boxplots_results(tqa_p_means, tqa_p_ratios, tqa_p_approx_ratios, prefix='TQA ')
from warmstart_helper import *
ws_p_means = []
ws_p_ratios = []
ws_p_approx_ratios = []
ws_opt_epsilons = []
for p in range(1,max_p+1):
opt_epsilon = optimize_epsilon(max_cut, reps=p)
m, r, ar = start_ws_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p, epsilon=opt_epsilon)
ws_p_means.append(m)
ws_p_ratios.append(r)
ws_p_approx_ratios.append(ar)
ws_opt_epsilons.append(opt_epsilon)
all_results["WarmStart QAOA"] = [ws_p_means, ws_p_ratios, ws_p_approx_ratios, ws_opt_epsilons]
ɛ=0.0 p=1: ..................................................
ɛ=0.0 p=2: ..................................................
ɛ=0.0 p=3: ..................................................
ɛ=0.0 p=4: ..................................................
ɛ=0.0 p=5: ..................................................
ɛ=0.0 p=6: ..................................................
ɛ=0.0 p=7: ..................................................
ɛ=0.0 p=8: ..................................................
ɛ=0.0 p=9: ..................................................
ɛ=0.0 p=10: ..................................................
display_boxplots_results(ws_p_means, ws_p_ratios, ws_p_approx_ratios, prefix='WarmStart ')
from recursive_qaoa_helper import *
recursive_p_means = []
recursive_p_ratios = []
recursive_p_approx_ratios = []
for p in range(1,max_p+1):
m, r, ar = start_recursive_evaluation(max_cut, eval_num=eval_num, reps=p)
recursive_p_means.append(m)
recursive_p_ratios.append(r)
recursive_p_approx_ratios.append(ar)
all_results["Recursive QAOA"] = [recursive_p_means, recursive_p_ratios, recursive_p_approx_ratios]
p=1: .................................................. p=2: .................................................. p=3: .................................................. p=4: .................................................. p=5: .................................................. p=6: .................................................. p=7: .................................................. p=8: .................................................. p=9: .................................................. p=10: ..................................................
display_boxplots_results(recursive_p_means, recursive_p_ratios, recursive_p_approx_ratios, prefix='Recursive ')
from recursive_ws_helper import *
ws_recursive_p_means = []
ws_recursive_p_ratios = []
ws_recursive_p_approx_ratios = []
for p in range(1,max_p+1):
opt_epsilon = ws_opt_epsilons[p-1]
m, r, ar = start_recursive_ws_qaoa_evaluation(max_cut, eval_num=eval_num, reps=p, epsilon=opt_epsilon)
ws_recursive_p_means.append(m)
ws_recursive_p_ratios.append(r)
ws_recursive_p_approx_ratios.append(ar)
all_results["Recursive WarmStrart QAOA"] = [ws_recursive_p_means, ws_recursive_p_ratios, ws_recursive_p_approx_ratios, ws_opt_epsilons]
p=1: .................................................. p=2: .................................................. p=3: .................................................. p=4: .................................................. p=5: .................................................. p=6: .................................................. p=7: .................................................. p=8: .................................................. p=9: .................................................. p=10: ..................................................
display_boxplots_results(ws_recursive_p_means, ws_recursive_p_ratios, ws_recursive_p_approx_ratios, prefix='Recursive WarmStart ')
# Save results
with open(f'comparison_simulator_{graph.name}_results.npy', 'wb') as f:
np.save(f, all_results)
from results_helper import *
means_df, ratio_df, approx_ratios_df = generate_dataframes(all_results)
means_df
| QAOA | TQA QAOA | WarmStart QAOA | Recursive QAOA | Recursive WarmStrart QAOA | |
|---|---|---|---|---|---|
| p | |||||
| 1 | -30.18 | -28.74 | -35.0 | -34.32 | -35.0 |
| 2 | -30.73 | -29.17 | -35.0 | -34.68 | -35.0 |
| 3 | -31.09 | -30.43 | -35.0 | -34.86 | -35.0 |
| 4 | -30.89 | -30.72 | -35.0 | -34.82 | -35.0 |
| 5 | -30.96 | -29.51 | -35.0 | -34.72 | -35.0 |
| 6 | -30.79 | -30.54 | -35.0 | -34.86 | -35.0 |
| 7 | -30.38 | -29.78 | -35.0 | -34.62 | -35.0 |
| 8 | -29.99 | -29.63 | -35.0 | -34.88 | -35.0 |
| 9 | -30.20 | -31.01 | -35.0 | -34.70 | -35.0 |
| 10 | -29.86 | -29.06 | -35.0 | -34.58 | -35.0 |
ratio_df
| QAOA | TQA QAOA | WarmStart QAOA | Recursive QAOA | Recursive WarmStrart QAOA | |
|---|---|---|---|---|---|
| p | |||||
| 1 | 0.86 | 0.82 | 0.0 | 0.0 | 0.0 |
| 2 | 0.87 | 0.83 | 0.0 | 0.0 | 0.0 |
| 3 | 0.89 | 0.87 | 0.0 | 0.0 | 0.0 |
| 4 | 0.88 | 0.88 | 0.0 | 0.0 | 0.0 |
| 5 | 0.88 | 0.84 | 0.0 | 0.0 | 0.0 |
| 6 | 0.88 | 0.87 | 0.0 | 0.0 | 0.0 |
| 7 | 0.87 | 0.85 | 0.0 | 0.0 | 0.0 |
| 8 | 0.86 | 0.85 | 0.0 | 0.0 | 0.0 |
| 9 | 0.86 | 0.89 | 0.0 | 0.0 | 0.0 |
| 10 | 0.85 | 0.83 | 0.0 | 0.0 | 0.0 |
approx_ratios_df
| QAOA | TQA QAOA | WarmStart QAOA | Recursive QAOA | Recursive WarmStrart QAOA | |
|---|---|---|---|---|---|
| p | |||||
| 1 | 0.86 | 0.82 | 1.0 | 0.98 | 1.0 |
| 2 | 0.88 | 0.83 | 1.0 | 0.99 | 1.0 |
| 3 | 0.89 | 0.87 | 1.0 | 1.00 | 1.0 |
| 4 | 0.88 | 0.88 | 1.0 | 0.99 | 1.0 |
| 5 | 0.88 | 0.84 | 1.0 | 0.99 | 1.0 |
| 6 | 0.88 | 0.87 | 1.0 | 1.00 | 1.0 |
| 7 | 0.87 | 0.85 | 1.0 | 0.99 | 1.0 |
| 8 | 0.86 | 0.85 | 1.0 | 1.00 | 1.0 |
| 9 | 0.86 | 0.89 | 1.0 | 0.99 | 1.0 |
| 10 | 0.85 | 0.83 | 1.0 | 0.99 | 1.0 |